<?php
if ('itg-em-admin-ajax.php' == basename($_SERVER['SCRIPT_FILENAME']))
    die ('<h2>Direct File Access Prohibited</h2>');
/**
 * Deals with all AJAX requests
 */
/**
 * The Admin side ajax function
 * Used to fetch Event Detail
 * uses thickbox
 * @global object $wpdb
 * @global array $_POST['item_id']
 */
function itg_em_ajax_ev_detail_cb() {
    global $wpdb, $itg_em_db_table_name;
    
    /** Shortcuts please */
    $mem_details = array(
        0 => '(Optional)',
        1 => '(Required)'
    );
    if(isset($_GET['item_id'])) {
        $ev_detail = itg_em_admin_event_detail($_GET['item_id']);
        
        if($ev_detail) {
            ?>
<button id="print">Print The Details</button>
<div id="printable">
<table class="widefat">
    <thead>
        <tr>
            <th scope="col" colspan="2">Details about the event: <?php echo $ev_detail->event_name; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>Event Name:</th>
            <td><?php echo $ev_detail->event_name; ?></td>
        </tr>
        <tr>
            <th>Event Description:</th>
            <td>
                <div class="ui-widget">
                        <div class="ui-state-highlight ui-corner-all" style="margin: 20px 0; padding:.7em;"> 
                                <?php echo $ev_detail->event_desc; ?>
                        </div>
                </div>
            </td>
        </tr>
        <tr>
            <th>Venue:</th>
            <td>
                <div class="ui-widget">
                        <div class="ui-state-highlight ui-corner-all" style="margin: 20px 0; padding:.7em;"> 
                                <?php echo $ev_detail->venue; ?>
                        </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>Team Members:</td>
            <td>
                <?php echo $ev_detail->team_mem; ?> <?php echo $mem_details[$ev_detail->mem_op]; ?>
            </td>
        </tr>
        <tr>
            <td>Total Rounds:</td>
            <td><?php echo $ev_detail->round; ?></td>
        </tr>
        <tr>
            <td>Price</td>
            <td>
                <?php
                if(-1.00 == $ev_detail->price)
                    echo 'Announced Later';
                else if(0.00 == $ev_detail->price)
                    echo 'Free Event';
                else
                    echo $ev_detail->price;
                ?>
            </td>
        </tr>
        <tr>
            <td>Reference:</td>
            <td>
                <?php
                if($ev_detail->ref != '') {
                    echo '<a href="' . $ev_detail->ref . '" class="button-secondary" target="_blank">Read More</a>';
                }
                else {
                    echo 'Not available';
                }
                ?>
            </td>
        </tr>
    </tbody>
</table>
<p style="font-size: 8px; font-style: italic">Generated by WP Simple Event management system - Dev. Swashata</p>
</div>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#print').click(function(){
            $('#printable').printArea({mode : 'popup', popClose: false});
        });
    });
</script>
            <?php
        }
        else {
            ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> No detail available at this time.</p>
    </div>
</div>
            <?php
        }
    }
    else {
        ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> No detail available at this time.</p>
    </div>
</div>
        <?php
    }
    die();
}
/**
 *Hook it
 */
add_action('wp_ajax_itg_em_event_detail_ajax', 'itg_em_ajax_ev_detail_cb');

/**
 * Shows the application status
 * Only if complete
 */
function itg_em_ajax_user_app_status_cb() {
    global $wpdb, $itg_em_db_table_name, $itg_em_options;
    
    if(!$_GET['item_id']) {
        ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> No detail available at this time.</p>
    </div>
</div>
        <?php
    }
    else {
        $app_detail = itg_em_user_app_detail($_GET['item_id']);
        
        if($app_detail) {
            $user_data = itg_em_admin_user_info($app_detail->uid);
            
            /**
             * Check to see if the current logged user is the one who have applied
             * If not, then user should be admin
             * else f**k him ;)
             */
            if(!current_user_can('itg_em_cap_admin')) {
                /** Not admin, check */
                if($_SESSION['itg_em_uid'] != $user_data->id) {
                    ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> Sorry cowboy! Only admins are supposed to do something like this, not you! - Swashata</p>
    </div>
</div>
                    <?php
                    die();
                }
            }
            ?>
<button id="print">Print All details</button>
<div id="printable">
<table class="widefat">
    <thead>
        <tr>
            <th scope="col" colspan="2">Details about your application</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th scope="col" colspan="2"><?php echo $itg_em_options['institute_name']; ?></th>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <th scope="col" colspan="2">Application Details:</th>
        </tr>
        <tr>
            <td>Application ID:</td>
            <td><?php echo $app_detail->id; ?></td>
        </tr>
        <tr>
            <td>Payment Status:</td>
            <td>
                <?php
                if(0 == $app_detail->pay_status)
                    echo 'Unpaid';
                else
                    echo 'Paid';
                ?>
            </td>
        </tr>
        <tr>
            <td>Application Status:</td>
            <td>
                <?php
                if(0 == $app_detail->user_apply)
                    echo 'Closed';
                else
                    echo 'Open';
                ?>
            </td>
        </tr>
        <tr>
            <td>Registration ID:</td>
            <td>
                <?php
                if(0 == $app_detail->pay_status && false == current_user_can('itg_em_cap_admin'))
                    echo 'Payment not received yet';
                else
                    echo $app_detail->reg_id;
                ?>
            </td>
        </tr>
        <tr>
            <th scope="col" colspan="2">Applicant Information:</th>
        </tr>
        <tr>
            <td>User IDs:</td>
            <td>(UID)<?php echo $app_detail->uid; ?><br />(WordPress UID)<?php echo $user_data->wp_uid; ?></td>
        </tr>
        <tr>
            <td>User Name:</td>
            <td><?php echo $user_data->first_name . ' ' . $user_data->last_name; ?> a.k.a (nickname) <?php echo $user_data->nickname; ?></td>
        </tr>
        <tr>
            <td>WordPress User Name:</td>
            <td><?php echo $user_data->user_login; ?></td>
        </tr>
        <tr>
            <td>User Email:</td>
            <td><?php echo $user_data->email; ?></td>
        </tr>
        <tr>
            <td>User Institution:</td>
            <td><?php echo $user_data->univ; ?></td>
        </tr>
        <tr>
            <td>Session Year - Department:</td>
            <td><?php echo $user_data->year; ?> - <?php echo $user_data->dept; ?></td>
        </tr>
        <tr>
            <td>Roll Number:</td>
            <td><?php echo $user_data->roll_no; ?></td>
        </tr>
        <tr>
            <td>Team Members:</td>
            <td>
                <ol>
                    <?php
                    if($app_detail->tem_mem) {
                        foreach($app_detail->tem_mem as $tem_mem) {
                            ?>
                            <li><?php echo $tem_mem->first_name . ' ' . $tem_mem->last_name; ?></li>
                            <?php
                        }
                    }
                    else {
                        ?>
                        <li>No team member selected/necessary for this event</li>
                        <?php
                    }
                    ?>
                </ol>
            </td>
        </tr>
        <tr>
            <th scope="col" colspan="2">Event Details:</th>
        </tr>
        <tr>
            <td>Event ID:</td>
            <td><?php echo $app_detail->event_id; ?></td>
        </tr>
        <tr>
            <td>Event Name:</td>
            <td><?php echo $app_detail->event_name; ?></td>
        </tr>
        <tr>
            <td>Event Timing:</td>
            <td>
                From <em><?php echo date('l jS \of F Y', strtotime($app_detail->start_date)); ?></em> <br /> To <em><?php echo date('l jS \of F Y', strtotime($app_detail->end_date)); ?></em>
            </td>
        </tr>
        <tr>
            <td>Event Details:</td>
            <td>
                <div class="ui-widget">
                        <div class="ui-state-highlight ui-corner-all" style="margin: 20px 0; padding:.7em;"> 
                                <?php echo $app_detail->event_desc; ?>
                        </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>Venue:</td>
            <td>
                <div class="ui-widget">
                        <div class="ui-state-highlight ui-corner-all" style="margin: 20px 0; padding:.7em;"> 
                                <?php echo $app_detail->venue; ?>
                        </div>
                </div>
            </td>
        </tr>
    </tbody>
</table>
<p style="font-size: 8px; font-style: italic">Generated by WP Simple Event management system - Dev. Swashata</p>
</div>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#print').click(function(){
            $('#printable').printArea({mode : 'popup', popClose: false});
        });
    });
</script>
            <?php
        }
        else {
            ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> No detail available at this time.</p>
    </div>
</div>
            <?php
        }
    }
    die();
}
/**
 * Hook it
 */
add_action('wp_ajax_itg_em_event_app_detail_ajax', 'itg_em_ajax_user_app_status_cb');


/**
 * Function to generate List of attendees
 * Based on their UID
 * @param int $id The UID [user_table] of the user
 * @global $itg_em_db_table_name
 * @global $wpdb
 * @return string The <tr><td>...</td></tr> structure of attendees
 */
function itg_em_admin_ajax_list_atd_cb() {
    global $wpdb, $itg_em_db_table_name, $itg_em_options;
    
    if(!current_user_can('itg_em_cap_admin')) {
        ?>
<div class="ui-widget">
    <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
        <strong>Error:</strong> Holy **** You are not authorized to do such thing!.</p>
    </div>
</div>
        <?php
        die();
    }
    /**
     * Get the POST DATA
     */
    $id = $_POST['id'];
    if(!$id) {
        ?>
        <tr>
            <td colspan="8">No data available for this request</td>
        </tr>
        <?php
    }
    /**
     * Okay take it seriously
     * First we get all the user information
     */
    $user_info = itg_em_admin_user_info($id);
    
    /** Now we get the Registration + Event Information */
    $reg_info = $wpdb->get_results($wpdb->prepare("SELECT {$itg_em_db_table_name['reg_table']}.id, uid, team_ids, event_id, reg_id, pay_status, user_apply, note, date, event_name FROM {$itg_em_db_table_name['reg_table']} LEFT JOIN {$itg_em_db_table_name['admin_event']} ON {$itg_em_db_table_name['reg_table']}.event_id = {$itg_em_db_table_name['admin_event']}.id WHERE {$itg_em_db_table_name['reg_table']}.uid = %d", $id));
    
    if($reg_info) {
        /** First count it */
        $row_span = count($reg_info);
        
        /**
         * Start the output
         * First the user
         */
        ?>
        <tr>
            <td rowspan="<?php echo $row_span; ?>">
                <ol>
                    <li><strong>User ID:</strong> <?php echo $user_info->id; ?></li>
                    <li><strong>User Name:</strong> <?php echo $user_info->first_name . ' ' . $user_info->last_name; ?> a.k.a (nickname) <?php echo $user_info->nickname; ?></li>
                    <li><strong>WordPress ID:</strong> <?php echo $user_info->wp_uid; ?></li>
                    <li><strong>WordPress username:</strong> <?php echo $user_info->user_login; ?></li>
                    <li><strong>Email:</strong> <?php echo $user_info->email; ?></li>
                    <li><strong>University:</strong> <?php echo $user_info->univ; ?></li>
                    <li><strong>Dept. - Session</strong> <?php echo $user_info->dept . ' - ' . $user_info->year; ?></li>
                    <li><strong>Roll Number:</strong> <?php echo $user_info->roll_no; ?></li>
                    <li><strong>Ph. Number:</strong> <?php echo $user_info->ph_no; ?></li>
                </ol>
            </td>
        <?php
        
        /** Now the event details */
        foreach($reg_info as $reg) {
            ?>
            <td><?php echo $reg->event_id . ' - ' . $reg->event_name; ?></td>
            <td><?php echo $reg->id; ?></td>
            <td><?php echo $reg->reg_id; ?></td>
            <td>
                <?php echo (($reg->pay_status == 0)? 'UnPaid' : 'Paid'); ?> and
                <?php echo (($reg->user_apply == 0)? 'Close' : 'Open'); ?>
            </td>
            <td>
                <ol>
                <?php
                if($reg->team_ids != '') {
                    $tem_mem = itg_em_admin_list_members($reg->team_ids);
                    foreach($tem_mem as $mem) {
                        ?>
                        <li>
                        <strong>Name:</strong> <?php echo $mem->first_name . ' ' ,$mem->last_name; ?>
                        <br />
                        <strong>Email:</strong> <?php echo $mem->email; ?>
                        <br />
                        <strong>Phone Number:</strong> <?php echo $mem->ph_no; ?>
                        </li>
                        <?php
                    }
                }
                else {
                    ?>
                    <li>No team member selected/required</li>
                    <?php
                }
                ?>
                </ol>
            </td>
            <td><?php echo date('jS M Y', strtotime($reg->date)); ?></td>
            <td><?php echo $reg->note; ?></td>
            </tr>
        <tr>
            <?php
        }
        ?>
            <td colspan="8" class="ui-state-default ui-corner-all" align="center"> ~~ End ~~ generated by - WP Event Management System [iTg]</td>
        </tr>
        <?php
    }
    else {
        ?>
        <tr>
            <td colspan="8">No data available for this request</td>
        </tr>
        <?php
    }
    die();
}
/**
 * Hook It
 */
add_action('wp_ajax_itg_em_list_atd_ajax', 'itg_em_admin_ajax_list_atd_cb');

/**
 * View all the team members of a user
 * @param int $id The uid of the user
 * @global $wpdb, $itg_em_db_table_name
 */
function itg_em_ajax_admin_user_team_mem_cb() {
    if(!isset($_GET['uid'])) {
        ?>
        <div class="error fade">Sorry! No user specified.</div>
        <?php
        die();
    }
    
    if(!current_user_can('itg_em_cap_admin')) {
        ?>
        <div class="error fade">Aah! Cought you! :D :P</div>
        <?php
        die();
    }
    
    /**
     * All fun part done
     * Now the serious part
     * We just get in the user team members
     * and echo inside a table
     */
    global $wpdb, $itg_em_db_table_name;
    ?>
    <table class="widefat">
        <thead>
            <tr>
                <th scope="col">Member ID</th>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Email</th>
                <th scope="col">Phone Number</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th scope="col">Member ID</th>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Email</th>
                <th scope="col">Phone Number</th>
            </tr>
        </tfoot>
        <tbody>
            <?php
            $team_members = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$itg_em_db_table_name['user_team']} WHERE itgem_uid = %d", $_GET['uid']));
            
            if($team_members) {
                foreach($team_members as $team) {
                    ?>
                    <tr>
                        <td><?php echo $team->id; ?></td>
                        <td><?php echo $team->first_name; ?></td>
                        <td><?php echo $team->last_name; ?></td>
                        <td><?php echo $team->email; ?></td>
                        <td><?php echo $team->ph_no; ?></td>
                    </tr>
                    <?php
                }
            }
            else {
                ?>
                <tr>
                    <td colspan="5">No Team Member exists</td>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>
    <?php
    die();
}
/**
 * Hook it
 */
add_action('wp_ajax_itg_em_ajax_admin_user_team', 'itg_em_ajax_admin_user_team_mem_cb');

/**
 * Similar function to list down the detail of a particular use
 * @param int $id the id of the user
 */
function itg_em_ajax_admin_single_user_cb() {
    if(!isset($_GET['uid'])) {
        ?>
        <div class="error fade">Sorry! No user specified.</div>
        <?php
        die();
    }
    
    if(!current_user_can('itg_em_cap_admin')) {
        ?>
        <div class="error fade">Aah! Cought you! :D :P</div>
        <?php
        die();
    }
    
    /**
     * All fun part done
     * Now the serious part
     * We seperate everything
     * 1. Identification Numbers, Name, Logins
     * 2. Contact Information
     * 3. Team Members
     * and echo inside a table
     */
    global $wpdb, $itg_em_db_table_name;
    
    /** Previous function to generate user information */
    $user_info = itg_em_admin_user_info($_GET['uid']);
    
    if($user_info) {
        ?>
        <button id="print">Print All details</button>
        <div id="printable">
        <table class="widefat">
            <thead>
                <tr>
                    <th scope="col">Specification</th>
                    <th scope="col">Details</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th colspan="2" scope="col">Information of user <?php echo $user_info->user_login; ?> [<?php echo $user_info->first_name . ' ' . $user_info->last_name; ?>] ~ Generated by WP Simple Event Manager - Dev. Swashata</th>
                </tr>
            </tfoot>
            <tbody>
                <tr>
                    <th scope="col" colspan="2">Identification Details</th>
                </tr>
                <tr>
                    <td>User ID:</td>
                    <td><?php echo $user_info->id; ?></td>
                </tr>
                <tr>
                    <td>WordPress ID:</td>
                    <td><?php echo $user_info->wp_uid; ?></td>
                </tr>
                <tr>
                    <td>Login Name:</td>
                    <td><?php echo $user_info->user_login; ?></td>
                </tr>
                <tr>
                    <td>First Name:</td>
                    <td><?php echo $user_info->first_name; ?></td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td><?php echo $user_info->last_name; ?></td>
                </tr>
                <tr>
                    <td>NickName:</td>
                    <td><?php echo $user_info->nickname; ?></td>
                </tr>
                <tr>
                    <th scope="col" colspan="2">Contact Information</th>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><a href="mailto:<?php echo $user_info->email; ?>"><?php echo $user_info->email; ?></a></td>
                </tr>
                <tr>
                    <td>Phone Number:</td>
                    <td><?php echo $user_info->ph_no; ?></td>
                </tr>
                <tr>
                    <td>Address:</td>
                    <td><?php echo $user_info->address; ?></td>
                </tr>
                <tr>
                    <td>State:</td>
                    <td><?php echo $user_info->state; ?></td>
                </tr>
                <tr>
                    <td>City:</td>
                    <td><?php echo $user_info->city; ?></td>
                </tr>
                <tr>
                    <td>Country:</td>
                    <td><?php echo $user_info->country; ?></td>
                </tr>
                <tr>
                    <th scope="col" colspan="2">Team Members</th>
                </tr>
                <?php
                $team_members = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$itg_em_db_table_name['user_team']} WHERE itgem_uid = %d", $_GET['uid']));
                
                if($team_members) {
                    foreach($team_members as $team) {
                        ?>
                <tr>
                    <td>Member ID [<?php echo $team->id; ?>]</td>
                    <td>
                        <ol>
                            <li>
                                <strong>Name:</strong> <?php echo $team->first_name . ' ' . $team->last_name; ?>
                            </li>
                            <li>
                                <strong>Email:</strong> <?php echo $team->email; ?>
                            </li>
                            <li>
                                <strong>Phone Number:</strong> <?php echo $team->ph_no; ?>
                            </li>
                        </ol>
                    </td>
                </tr>
                        <?php
                    }
                }
                else {
                    ?>
                    <tr>
                        <td colspan="2">No Team member added by this user yet</td>
                    </tr>
                    <?php
                }
                ?>
            </tbody>
        </table>
        </div>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#print').click(function(){
            $('#printable').printArea({mode : 'popup', popClose: false});
        });
    });
</script>
        <?php
    }
    else {
        ?>
        <div class="error fade">Could not fetch user details</div>
        <?php
    }
    die();
}
/**
 * Hook it
 */
add_action('wp_ajax_itg_em_ajax_admin_user_info', 'itg_em_ajax_admin_single_user_cb');